package cn.itcast.usermanage.controller;



import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import cn.itcast.usermanage.pojo.User;
import cn.itcast.usermanage.service.UserService;

@Controller
//@RequestMapping("rest/user")
public class MyController {
    @Autowired
	private  UserService   userservice;
	
	@RequestMapping(value="{id}",method=RequestMethod.GET)
	public    ResponseEntity<User> queryUserId(@PathVariable("id") Long  id){
		try {
			if (id.longValue() < 1) {
				return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(null);
			}
			User user = this.userservice.queryUserById(id);
			if (null == user) {
				return ResponseEntity.status(HttpStatus.NOT_FOUND).body(null);

			}
			return ResponseEntity.ok(user);
		} catch (Exception e) {
			e.printStackTrace();
					
		}
		return  ResponseEntity.status(HttpStatus.BAD_REQUEST).body(null);
		
	}  
	
	
	
	
	@RequestMapping(method=RequestMethod.POST)
	public  ResponseEntity<Void>  saveUser(User user){
		try {
			/*if (StringUtil*/
			if (StringUtils.isEmpty(user.getName() == null)) {
				return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
			}
			Boolean saveUser = this.userservice.saveUser(user);
			System.err.println(saveUser);
			if(saveUser){
				System.err.println("执行");
				return ResponseEntity.status(HttpStatus.CREATED).build();}
			System.err.println("不执行把");
			return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
		} catch (Exception e) {
			e.printStackTrace();
			System.err.println("不执行把");
			return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
		}	
	}
	
	@PutMapping
	public    ResponseEntity<Void> updateUser(User user){
		try {
			if(user.getId()==null||user.getId().longValue()<0){
				return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();				
			}
			int  index=this.userservice.updateUser(user);
		if(index==1){
			System.err.println(index);
			return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
		}
		else{
			System.err.println(index);
		return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
		}
		
		} catch (Exception e) {
			return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
		}
		
	}
	
	
	
/*	@DeleteMapping
	public  ResponseEntity<Void> deleteUser(@RequestParam(value="id",defaultValue="0")Long id){
		try {
			if (id.intValue() == 0) {
				return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
			}
			Integer count = this.userservice.deleteUser1(id);
			if (count.intValue() == 1) {
				return ResponseEntity.status(HttpStatus.NO_CONTENT).build();

			} else {
				return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
			}
			//500
		} catch (Exception e) {
			e.printStackTrace();
			return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
		}
	}
	*/
	
	@DeleteMapping
	public  ResponseEntity<Void> deleteUsers(@RequestParam(value="ids",defaultValue="[]")Long[] ids){
		try {
			if (ids == null || ids.length == 0) {
				return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
			}
			// 执行
			this.userservice.deleteUsers(ids);
			return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
		} catch (Exception e) {
			e.printStackTrace();
			 return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
		}		
		
		
	}
	
	
/*	@DeleteMapping
	public  ResponseEntity<Void> deleteUsers(@RequestParam(value="ids",defaultValue="[]")List<Object> ids){
		try {
			if (ids == null ) {
				return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
			}
			// 执行
			this.userservice.deleteUsers(ids);
			return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
		} catch (Exception e) {
			e.printStackTrace();
			 return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
		}		
		
		
	}
	*/
	
	
}
